//
//  MOChangeGesturPasswordController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/3.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOChangeGesturPasswordController.h"
#import "GesturePasswordView.h"


@interface MOChangeGesturPasswordController ()

@property(nonatomic,strong)UIView *headerView;

@property(nonatomic,strong)UILabel *titleLabel;

@property(strong,nonatomic)GesturePasswordView *gesturePasswordView;

@end

@implementation MOChangeGesturPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNav];
    
    [self creatView];
}

-(void)initNav
{
    if (self.status==0) {//设置密码
        self.title = @"设置手势密码";
    }else if (self.status==1){//修改手势密码
        self.title = @"修改手势密码";
    }else{//绘制登录密码
        self.title = @"绘制手势密码";
    }
    
    
    self.view.backgroundColor = [UIColor whiteColor];
}

-(void)creatView
{
    [self.view addSubview:self.headerView];
    
    [self creatGestureView];
    
}

-(void)creatGestureView
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
    __weak __typeof(&*self) weakSelf = self;
    if (self.status == GesturePasswordStatusSet) {
        self.titleLabel.text = @"请设置手势密码";

        GesturePasswordView *gesturePasswordView = [GesturePasswordView status:GesturePasswordStatusSet frame:CGRectMake(0, self.headerView.bottom, SCREEN_WIDTH, SCREEN_WIDTH-100) onPasswordSet:^() {
            weakSelf.titleLabel.text = @"请重新输入刚才设置的手势密码";
        } onGetCorrectPswd:^ {
            weakSelf.titleLabel.text = @"设置成功";
            [weakSelf setGesturePasswordData];

        } onGetIncorrectPswd:^ {
            weakSelf.titleLabel.text = @"与上一次输入不一致，请重新设置";
        } errorInput:^{
            weakSelf.titleLabel.text = @"请至少连接4个点";
        }];
        [self.view addSubview:gesturePasswordView];
    }else if (self.status == GesturePasswordStatusReset){
        self.titleLabel.text = @"请验证旧密码";

        GesturePasswordView *gesturePasswordView = [GesturePasswordView status:GesturePasswordStatusReset frame:CGRectMake(0, self.headerView.bottom, SCREEN_WIDTH, SCREEN_WIDTH-100) verificationPassword:^{
            weakSelf.titleLabel.text = @"请输入新的手势密码";
        } verificationError:^{
            weakSelf.titleLabel.text = @"旧密码验证错误";
        }  onPasswordSet:^ {
            weakSelf.titleLabel.text = @"请重新输入刚才设置的手势密码";
        } onGetCorrectPswd:^ {
            weakSelf.titleLabel.text = @"设置成功";
            [weakSelf setGesturePasswordData];
        } onGetIncorrectPswd:^ {
            weakSelf.titleLabel.text = @"与上一次输入不一致，请重新设置";
        } errorInput:^{
            weakSelf.titleLabel.text = @"请至少连接4个点";
        }];
        [self.view addSubview:gesturePasswordView];
    }else if (self.status == GesturePasswordStatusLogin){
        self.titleLabel.text = @"请输入手势密码";
        GesturePasswordView *gesturePasswordView = [GesturePasswordView status:GesturePasswordStatusLogin frame:CGRectMake(0, self.headerView.bottom, SCREEN_WIDTH, SCREEN_WIDTH-100) onGetCorrectPswd:^ {
            weakSelf.titleLabel.text = @"解锁成功";
//            [weakSelf loginSuccess];
        } onGetIncorrectPswd:^ {
            weakSelf.titleLabel.text = @"密码错误，请重新输入";
        } errorInput:^{
            weakSelf.titleLabel.text = @"请至少连接4个点";
        }];
        [self.view addSubview:gesturePasswordView];
    }
}

#pragma mark -- 设置手势密码/重置手势密码
-(void)setGesturePasswordData{

    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:GESTUREPASSWORD];
    NSString *pas = [[dic objectForKey:@"password"] deentryptAESBase64];
    
    NSString *postUrl = [NSString stringWithFormat:@"%@/%@/%@?authorization=%@",ADDLOCKER_URL,pas,[MOUser sharedMOUser].userName,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"设置手势密码:%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject integerValue]==1) {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
          [[ADShowMessage shareShowMessage] showTextOnly:@"设置失败，稍后重试" showInView:self.view];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}




#pragma mark -- 懒加载
-(UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160)];
//        _headerView.backgroundColor = KGrayColor_f8f8f8;
        
        UIImageView *headerImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 35, 25, 25)];
        headerImage.image = IMAGE(@"gesturPassShow");
        headerImage.centerX = SCREEN_WIDTH/2;
        [_headerView addSubview:headerImage];
        
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, headerImage.bottom + 10, SCREEN_WIDTH - 30, _headerView.height-headerImage.bottom-10-20)];
        self.titleLabel.font = FONT_SIZE(13);
        self.titleLabel.textColor = KBlackColor_2A2A2A;
        self.titleLabel.text = @"请绘制解锁图案";
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_headerView addSubview:self.titleLabel];
    }
    return _headerView;
}

@end
